
import { UI_Listen, UI_Layer } from '../../config/ui_def'
import { WX_AD_CONFIG, Sdk_Platform, VIVO_AD_CONFIG, OPPO_AD_CONFIG } from '../../frame/constants';
import numeral from '../base/numeral'
cc.Class({
    extends: cc.Component,

    properties: {
        title_lab: cc.Label,
        item_content_node: cc.Node,
        star_node: cc.Node,
        yellowStar_node: cc.Node,
        unLock_node: cc.Node,
        itemScroll_node: cc.Node,
        passNode: cc.Node,
        adNode: cc.Node,
        adIcon: cc.Sprite,
        adTitle: cc.Label,
        adDescribe: cc.Label,
    },

    // LIFE-CYCLE CALLBACKS:
    initInfo(passinfo) {
        if (!he.pool.getPool('bagItem')) {
            he.pool.createPoolSync('bagItem', 5, cc.Prefab, 'uimod/bagItem')
        }
        let passList = he.local.getJson('pass_list', 'user') || []
        this.passInfo = passinfo.info
        if (!passList) {
            // console.log('错误')
            return
        }
        if (this.passInfo.isAd) {
            this.passNode.active = false
            this.adNode.active = true
            this.adSwitch(true)
            return
        }
        this.node.active = true
        this.passNode.active = true
        this.adNode.active = false
        let isHave = false
        let passlit = passList.length + 1
        let passid = this.passInfo.order
        if (passid <= passlit) {
            // this.scrollInit(this.passInfo.id, passList[i].star)
            this.clearItem()
            this.islock = false
            for (let i = 0; i < passList.length; i++) {
                if (passList[i].id == this.passInfo.id) {
                    isHave = true
                    this.scrollInit(this.passInfo.id, passList[i].star)
                }
            }
            if (!isHave) {
                this.scrollInit(this.passInfo.id, 0)
            }
        } else {
            this.islock = true
            this.lockinit()
        }

    },
    //如果没有打过该关卡或者通关失败了  显示怪物情况，若成功通过一次就显示素材
    scrollInit(passId, star) {
        this.itemScroll_node.active = true
        this.unLock_node.active = false
        this.star_node.active = true
        this.title_lab.string = `${this.passInfo.name}`
        if (passId && star < 1) {
        } else if (passId && star >= 1) {
        }
        for (let i = 0; i < 3; i++) {
            this.yellowStar_node.children[i].active = false
        }
        for (let j = 0; j < star; j++) {
            this.yellowStar_node.children[j].active = true
        }
        this.mergeInit()
    },

    mergeInit() {


        let grp = this.passInfo.reward.split('#')
        for (let i = 0; i < grp.length; i++) {
            let pre = he.pool.getElement('bagItem')
            if (pre) {
                pre.parent = this.item_content_node
                pre.setPosition(cc.Vec2.ZERO)
                pre.getComponent('bagItem').hideItem()
                pre.getComponent('bagItem').hideLight()
            }
            let infoGrp = grp[i].split('_')
            this.item_content_node.children[i].active = true
            let itemScript = this.item_content_node.children[i].getComponent('bagItem')
            let item_id = parseInt(infoGrp[0])
            itemScript.initInfo(item_id)
            let num1 = this.getLabNum(infoGrp[1])
            let num2 = this.getLabNum(infoGrp[2])
            itemScript.setLabString(`${num1}~${num2}`)
        }
    },


    getLabNum(num) {
        let newNum = numeral(num)
        let value = newNum.format('0,a');
        return value
    },

    clearItem() {
        for (let i = (this.item_content_node.children.length); i > 0; i--) {
            this.item_content_node.children[i - 1].setScale(1)
            this.item_content_node.children[i - 1].setContentSize(112, 112)
            he.pool.putElement('bagItem', this.item_content_node.children[i - 1])
        }
    },

    lockinit() {
        this.title_lab.string = `${this.passInfo.name}`
        this.itemScroll_node.active = false
        this.unLock_node.active = true
        this.star_node.active = false
    },


    clickServer() {
        // console.log('xxxx')
        if (this.islock) {
            return
        }
        this.node.parent.parent.parent.parent.getComponent('passListPage').realClose()
        gg.uiManager.closePage(UI_Layer.Cover, 'rankMainPage')
        gg.eventManager.emit('main', { type: UI_Listen.MAIN_SHOWPASS, passId: (this.passInfo.id) })
    },
    //-------------------------------------------------------------------广告
    adclickServer() {
        switch (common.sdk_env) {
            case Sdk_Platform.WECHAT:
                // console.log('clockWXXXXXXX')
                this.wxClilkServer()
                break
            case Sdk_Platform.VIVO:
                he.ad.clickNative(VIVO_AD_CONFIG.nativeAd[4])
                break
            case Sdk_Platform.OPPO:
                break
        }
        // if (this.canClick) {
        // } else {
        // }
    },

    adSwitch(status) {
        // this.node.active = true
        if (status) {
            switch (common.sdk_env) {
                case Sdk_Platform.WECHAT:
                    break
                case Sdk_Platform.VIVO:
                    this.vivoAdInit()
                    break
                case Sdk_Platform.OPPO:
                    break
                default:
                    this.node.active = false
                    break
            }
        } else {
            //  关掉广告
        }
    },

    vivoAdInit() {
        if (this.passInfo.adInfo != null && this.passInfo.adInfo != undefined) {
            this.successServer(this.passInfo.adInfo)
        }
        else {
            this.failServer()
        }

    },

    successServer(res) {
        if (res == [] || res == null || res == undefined) {
            this.node.active = false
        } else {
            this.node.active = true
            this.loadImg(this.adIcon, res.imgUrlList[0])

            this.adDescribe.string = `${res.desc}`
            // console.log(this.nameLab.string)
            this.adTitle.string = `${res.title}`
            if (res.imgUrlList[0] == null || res.imgUrlList[0] == undefined) {
                this.node.active = false
            }
        }

    },

    failServer() {
        this.node.active = false
    },


    loadImg(container, url) {
        if (url.length < 4) {
            return he.error(("图片长度不对错误"));
        }
        let ext = url.substring(url.length - 4, url.length)
        ext = ext.toLowerCase()
        if (ext == 'jpeg') ext = '.jpg'
        cc.assetManager.loadRemote(url, { ext: ext }, (err, texture) => {
            if (err) {
                he.error(err)
            }
            if (this.callFun) {
                this.callFun()
            }
            var sprite = new cc.SpriteFrame(texture);
            container.spriteFrame = sprite;
            this.adIcon.node.setContentSize(120, 120)
        });
    },


});
